package com.shtel.paas.monitorcenter.autodiscover.controller;

import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmFrequency;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmKpi;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmResponse;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmRule;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmRulesHost;
import com.shtel.paas.monitorcenter.autodiscover.entity.AlarmWhite;
import com.shtel.paas.monitorcenter.autodiscover.entity.MonitorCenterHostInfomation;
import com.shtel.paas.monitorcenter.autodiscover.entity.RuleId;
import com.shtel.paas.monitorcenter.autodiscover.mapper.QueryInfoMapper;
import com.shtel.paas.monitorcenter.autodiscover.service.AutoDiscoverService;
import com.shtel.paas.monitorcenter.autodiscover.util.AutoFind;
import com.shtel.paas.monitorcenter.autodiscover.util.Cors;

@MapperScan("com.shtel.paas.monitorcenter.autodiscover.mapper")
@RestController
public class AutoDiscoverController {

	@Autowired
	AutoDiscoverService autoDiscoverService;


	/**
	 * 
	 * 查询指标规则
	 * 
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryIndexRule")
	@ResponseBody
	public String queryIndexRule() {

		// ListAlarmRuleDto data = new ListAlarmRuleDto();
		// data.setAlarmRules();
		// //String str = JSON.toJSONString(data);
		List<AlarmRule> data = autoDiscoverService.queryIndexRule();
		return JSON.toJSONString(data);

	}

	/**
	 * 
	 * 查询频率信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryFrequency", method = RequestMethod.GET)
	@ResponseBody
	public String queryFrequency() {
		List<AlarmFrequency> data = autoDiscoverService.queryFrequency();
		return JSON.toJSONString(data);

	}
	

	/**
	 * 
	 * 查询运维信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryKpi", method = RequestMethod.GET)
	@ResponseBody
	public String queryKpi() {

		List<AlarmKpi> data = autoDiscoverService.queryKpi();

		return JSON.toJSONString(data);

	}

	/**
	 * 
	 * 查询ruleid
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryRuleid", method = RequestMethod.GET)
	@ResponseBody
	public int queryRuleid() {
		String name = "cpu-use";
		int level = 1;
		RuleId ruleId = new RuleId();
		ruleId.setName(name);
		ruleId.setLevel(level);

		int ruleIdData = autoDiscoverService.queryRuleId(ruleId);

		return ruleIdData;

	}

	/**
	 * 返回prometheus接口获取的ips与库里ip的差集ip
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getIps", method = RequestMethod.GET)
	@ResponseBody
	public String getIps() {
		JSONObject data = autoDiscoverService.queryIps();
		return JSON.toJSONString(data);

	}

	/**
	 * 保存数据
	 * 
	 * @param object1
	 * @return
	 */
	@RequestMapping(value = "/saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public String saveInfo(@RequestBody MultiValueMap<String, String> param) {
//		@RequestBody MultiValueMap<String, String> param		
//		@RequestBody String object		
		Map<String, String> params = param.toSingleValueMap();
		String object1 = params.get("msg");
		
		String object = URLDecoder.decode(object1);
		
		return autoDiscoverService.saveInfo(object);

	}

}
